<template>
  <div>
    <a-dropdown style="margin-right: 10px">
      <a-menu slot="overlay" @click="handleMenuClick">
        <a-menu-item key="1">中益午免计划</a-menu-item>
        <a-menu-item key="2">益起汇</a-menu-item>
      </a-menu>
      <a-button style="margin-left: 8px"> {{ platformName }} <a-icon type="down" /> </a-button>
    </a-dropdown>
    <a-dropdown v-if="currentUser && currentUser.name" placement="bottomRight">
      <span class="ant-pro-account-avatar">
        <!-- <a-avatar size="small" src="https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png" class="antd-pro-global-header-index-avatar" /> -->
        <span>{{ currentUser.name }}</span>
      </span>
      <template v-slot:overlay>
        <a-menu class="ant-pro-drop-down menu" :selected-keys="[]">
          <!-- <a-menu-item v-if="menu" key="center" @click="handleToCenter">
            <a-icon type="user" />
            {{ $t('menu.account.center') }}
          </a-menu-item>
          <a-menu-item v-if="menu" key="settings" @click="handleToSettings">
            <a-icon type="setting" />
            {{ $t('menu.account.settings') }}
          </a-menu-item>
          <a-menu-divider v-if="menu" /> -->
          <a-menu-item key="logout" @click="handleLogout">
            <a-icon type="logout" />
            退出
          </a-menu-item>
        </a-menu>
      </template>
    </a-dropdown>
    <span v-else>
      <a-spin size="small" :style="{ marginLeft: 8, marginRight: 8 }" />
    </span>
  </div>
</template>

<script>
import { Modal } from 'ant-design-vue'
import storage from 'store'

export default {
  name: 'AvatarDropdown',
  props: {
    currentUser: {
      type: Object,
      default: () => null
    },
    menu: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      platformName: '中益午免计划'
    }
  },
  created() {
    const appletId = storage.get('AppletId') || '1'
    this.platformName = appletId === '1' ? '中益午免计划' : '益起汇'
  },
  methods: {
    handleToCenter() {
      this.$router.push({ path: '/account/center' })
    },
    handleToSettings() {
      this.$router.push({ path: '/account/settings' })
    },
    handleLogout(e) {
      Modal.confirm({
        title: '退出',
        content: '确定要退出吗？',
        onOk: () => {
          // return new Promise((resolve, reject) => {
          //   setTimeout(Math.random() > 0.5 ? resolve : reject, 1500)
          // }).catch(() => console.log('Oops errors!'))
          return this.$store.dispatch('Logout').then(() => {
            this.$router.push({ name: 'login' })
          })
        },
        onCancel() {}
      })
    },
    // 切换平台
    handleMenuClick(e) {
      const key = e.key
      storage.set('AppletId', key)
      this.platformName = key === '1' ? '中益午免计划' : '益起汇'
      console.log('p', storage.get('AppletId'))
      location.reload()
    }
  }
}
</script>

<style lang="less" scoped>
.ant-pro-drop-down {
  /deep/ .action {
    margin-right: 8px;
  }
  /deep/ .ant-dropdown-menu-item {
    min-width: 160px;
  }
}
</style>
